//
//  LAMapView+MKMapViewDelegate.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2017/4/8.
//  Copyright © 2017年 2Bulu. All rights reserved.
//

#import "LAGMapView.h"
#import "LAGMapProtocol.h"

@interface LAGMapView (MKMapViewDelegate)

@end

@implementation LAGMapView (MKMapViewDelegate)

#pragma mark- MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    
//    id obj2 = [mapView valueForKey:@"gestureController"];
//    UIPanGestureRecognizer *gr = [obj2 valueForKey:@"_panGestureRecognizer"];
//    NSMutableArray *target1 = [gr valueForKey:@"_targets"];
//
//    UIPinchGestureRecognizer *pgr = [obj2 valueForKey:@"_pinchGestureRecognizer"];
//    NSMutableArray *target2 = [pgr valueForKey:@"_targets"];
    
    if([_revisedDelegate respondsToSelector:@selector(mapView:regionWillChangeAnimated:)])
        [_revisedDelegate mapView:mapView regionWillChangeAnimated:animated];
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:regionDidChangeAnimated:)])
        [_revisedDelegate mapView:mapView regionDidChangeAnimated:animated];
    
    [self refreshAnnotationCluster];
    
}

- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewWillStartLoadingMap:)])
        [_revisedDelegate mapViewWillStartLoadingMap:mapView];
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)])
        [_revisedDelegate mapViewDidFinishLoadingMap:mapView];
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewDidFailLoadingMap:withError:)])
        [_revisedDelegate mapViewDidFailLoadingMap:mapView withError:error];
}

- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewWillStartRenderingMap:)])
        [_revisedDelegate mapViewWillStartRenderingMap:mapView];
}

- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered NS_AVAILABLE(10_9, 7_0)
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewDidFinishRenderingMap:fullyRendered:)])
        [_revisedDelegate mapViewDidFinishRenderingMap:mapView fullyRendered:fullyRendered];
}

// mapView:viewForAnnotation: provides the view for each annotation.
// This method may be called for all or some of the added annotations.
// For MapKit provided annotations (eg. MKUserLocation) return nil to use the MapKit provided annotation view.
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <LAGAnnotationExt>)annotation
{
    MKAnnotationView *annView = nil;
    if([_revisedDelegate respondsToSelector:@selector(mapView:viewForAnnotation:)])
        annView = [_revisedDelegate mapView:mapView viewForAnnotation:annotation];
    
    if(!annView)
    {
        NSString *reuseId = nil;
        
        if([annotation respondsToSelector:@selector(viewReuseIdentifer)])
            reuseId = [annotation viewReuseIdentifer];
        
        if(reuseId)
            annView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
        
       // if(annView)
            annView.annotation = annotation;
    }
    
    if(!annView && [annotation respondsToSelector:@selector(createAnnotationview)])
        annView = [annotation createAnnotationview];
        
    return annView;
}

// mapView:didAddAnnotationViews: is called after the annotation views have been added and positioned in the map.
// The delegate can implement this method to animate the adding of the annotations views.
// Use the current positions of the annotation views as the destinations of the animation.
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didAddAnnotationViews:)])
        [_revisedDelegate mapView:mapView didAddAnnotationViews:views];
}

#if TARGET_OS_IPHONE
// mapView:annotationView:calloutAccessoryControlTapped: is called when the user taps on left & right callout accessory UIControls.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:annotationView:calloutAccessoryControlTapped:)])
        [_revisedDelegate mapView:mapView annotationView:view calloutAccessoryControlTapped:control];
}
#endif

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didSelectAnnotationView:)])
        [_revisedDelegate mapView:mapView didSelectAnnotationView:view];
    
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didDeselectAnnotationView:)])
        [_revisedDelegate mapView:mapView didDeselectAnnotationView:view];
}

- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewWillStartLocatingUser:)])
        [_revisedDelegate mapViewWillStartLocatingUser:mapView];
}

- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView
{
    if([_revisedDelegate respondsToSelector:@selector(mapViewDidStopLocatingUser:)])
        [_revisedDelegate mapViewDidStopLocatingUser:mapView];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didUpdateUserLocation:)])
        [_revisedDelegate mapView:mapView didUpdateUserLocation:userLocation];
}

- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didFailToLocateUserWithError:)])
        [_revisedDelegate mapView:mapView didFailToLocateUserWithError:error];
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState
   fromOldState:(MKAnnotationViewDragState)oldState
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:annotationView:didChangeDragState:fromOldState:)])
        [_revisedDelegate mapView:mapView annotationView:view didChangeDragState:newState fromOldState:oldState];
}

#if TARGET_OS_IPHONE
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didChangeUserTrackingMode:animated:)])
        [_revisedDelegate mapView:mapView didChangeUserTrackingMode:mode animated:animated];
}
#endif

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayRenderer *renderer = nil;
    if([_revisedDelegate respondsToSelector:@selector(mapView:rendererForOverlay:)])
        renderer = [_revisedDelegate mapView:mapView rendererForOverlay:overlay];
    
    if(!renderer && [overlay respondsToSelector:@selector(renderer)])
    {
        renderer = [overlay performSelector:@selector(renderer) withObject:nil];
    }
    
    if(!renderer)
    {
        if([overlay isKindOfClass:[MKPolyline class]])
        {
            renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        }
        else if([overlay isKindOfClass:[MKPolygon class]])
        {
            renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
        }
        else if([overlay isKindOfClass:[MKCircle class]])
        {
            renderer = [[MKCircleRenderer alloc] initWithCircle:overlay];
        }
        
        if([renderer isKindOfClass:[MKOverlayPathRenderer class]])
        {
            MKOverlayPathRenderer *pathRenderer = (MKOverlayPathRenderer*)renderer;
            pathRenderer.lineWidth = 2.0;
#if TARGET_OS_IPHONE
            pathRenderer.strokeColor = [UIColor redColor];
            pathRenderer.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.25];
#else
            pathRenderer.strokeColor = [NSColor redColor];
            pathRenderer.fillColor = [[NSColor blueColor] colorWithAlphaComponent:0.25];
#endif
        }
    }
    
    
    return renderer;
}
- (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray<MKOverlayRenderer *> *)renderers
{
    if([_revisedDelegate respondsToSelector:@selector(mapView:didAddOverlayRenderers:)])
        [_revisedDelegate mapView:mapView didAddOverlayRenderers:renderers];
}

//#if TARGET_OS_IPHONE
//// Prefer -mapView:rendererForOverlay:
//- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
//{
//    MKOverlayView *overlayView = nil;
//    if([_revisedDelegate respondsToSelector:@selector(mapView:viewForOverlay:)])
//        overlayView = [_revisedDelegate mapView:mapView viewForOverlay:overlay];
//    
//    if(!overlayView)
//    {
//        
//    }
//    
//    return overlayView;
//}
//// Called after the provided overlay views have been added and positioned in the map.
//// Prefer -mapView:didAddOverlayRenderers:
//- (void)mapView:(MKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews
//{
//    if([_revisedDelegate respondsToSelector:@selector(mapView:didAddOverlayViews:)])
//        [_revisedDelegate mapView:mapView didAddOverlayViews:overlayViews];
//}
//#endif


@end
